﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Graphic = Ribbon.Core.Graphics.GraphicHelper;
using Ribbon.Core.Event;

namespace Ribbon.Core
{
    public class RibbonPageButton : RibbonPageBase
    {
        public event MouseClickDelegate MouseClick;

        public event MouseEnterDelegate MouseEnter;

        public event MouseMoveDelegate MouseMove;

        public event MouseLeaveDelegate MouseLeave;

        public RibbonPageButton(string name)
        {
            this.Name = name;
            this.Text = name; 
        }

        public RibbonPageButton(string name, string text)
            : this(name)
        {
            this.Text = text;
        }

        private float radiu;

        public override float Radiu
        {
            get
            {
                return radiu;
            }
            set
            {
                radiu = value;
            }
        }

        Ribbon parent;

        public override Ribbon Parent
        {
            get { return parent; }
            set
            {
                parent = value;

                if (this.Font == null)
                    this.Font = this.Parent.Font;

                GetSize();

                //TODO: Get Items Size

            }
        } 
         
        private Font font;

        public override Font Font
        {
            get
            {
                if (font == null)
                    return Parent.Font;
                else
                    return font;
            }
            set
            {
                font = value;
            }
        }

        public override string ToString()
        {
            return string.Format("Name:{0} IsActive:{1}", this.Name);
        }

        internal virtual void Refresh()
        {
            this.Parent.Refresh();
        }

        internal override void GetSize()
        {
            if (this.parent != null)
            {
                var s = Graphic.Graphics.MeasureString("", this.Font);

                if (s.Height > 0)
                {
                    int height = (int)(s.Height + 10 * DPIHelper.DPIScale);

                    if (this.parent.TabHeight < height)
                    {
                        this.parent.TabHeight = height;
                    }

                    float y = (this.parent.TabHeight - s.Height) / 2f;

                    this.Rect = new Rect(this.parent.TempEmptyX, y, s.Width + 5f, s.Height + 5f);
                }
                else
                {
                    int height = this.parent.TabHeight - (int)(10 * DPIHelper.DPIScale);

                    float y = (this.parent.TabHeight - height) / 2f;

                    this.Rect = new Rect(this.parent.TempEmptyX, y, s.Width, height);
                }

                if (this.Image != null)
                {
                    this.Rect.Width += 21f;
                }

                this.parent.TempEmptyX += this.Rect.Width + 5f;
            }
        }

        public override void OnPaint(System.Drawing.Graphics g)
        {
            if (this.Rect == null)
            {
                throw new Exception("none region");
            }

            if (this.IsMouseHover)
            {
                Brush bru_HearderBack = new SolidBrush(Color.FromArgb(
                    this.BackColor.R - 20 > 0 ? this.BackColor.R - 20 : 0,
                    this.BackColor.G - 20 > 0 ? this.BackColor.G - 20 : 0,
                    this.BackColor.B - 20 > 0 ? this.BackColor.B - 20 : 0));

                if (this.Radiu > 1)
                {
                    System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();

                    path.AddLine(this.Rect.X, this.Rect.Y + this.Rect.Height - this.Radiu, this.Rect.X, this.Rect.Y + this.Radiu);

                    path.AddArc(this.Rect.X, this.Rect.Y, this.Radiu, this.Radiu, 180, 90);

                    path.AddLine(this.Rect.X + this.Radiu, this.Rect.Y, this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y);

                    path.AddArc(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y, this.Radiu, this.Radiu, 270, 90);

                    path.AddLine(this.Rect.X + this.Rect.Width, this.Rect.Y + this.Radiu, this.Rect.X + this.Rect.Width, this.Rect.Y + this.Rect.Height - this.Radiu);

                    path.AddArc(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y + this.Rect.Height - this.Radiu, this.Radiu, this.Radiu, 0, 90);

                    path.AddLine(this.Rect.X + this.Rect.Width - this.Radiu, this.Rect.Y + this.Rect.Height, this.Rect.X + this.Radiu, this.Rect.Y + this.Rect.Height);

                    path.AddArc(this.Rect.X, this.Rect.Y + this.Rect.Height - this.Radiu, this.Radiu, this.Radiu, 90, 90);

                    g.FillPath(bru_HearderBack, path);

                    using (path) { }
                }
                else
                {
                    g.FillRectangle(bru_HearderBack, this.Rect.X, this.Rect.Y, this.Rect.Width, this.Rect.Height);
                }

                using (bru_HearderBack) { }
            }

            if (this.Image != null)
            {
                int imageScale = 16;

                float totalWidth = imageScale + this.TextSize.Width + 2f;

                float tempX = this.Rect.X + (this.Rect.Width - totalWidth) / 2;

                g.DrawImage(this.Image, new Rectangle((int)(tempX) + 3, (int)(this.Rect.Y + (this.Rect.Height - imageScale) / 2 + 0.5f), 16, 16), new Rectangle(0, 0, this.Image.Width, this.Image.Height), GraphicsUnit.Pixel);

                //using (Brush bru_Hearder = new SolidBrush(this.ForeColor))
                //{
                //    tempX += imageScale + 2f;

                //    g.DrawString(this.Text, Font, bru_Hearder, tempX, this.Rect.Y + (this.Rect.Height - this.TextSize.Height) / 2 + 1f);
                //}
            }
            else
            {
                //using (Brush bru_Hearder = new SolidBrush(this.ForeColor))
                //{
                //    g.DrawString(this.Text, Font, bru_Hearder, this.Rect.X + (this.Rect.Width - this.TextSize.Width) / 2 + 1f, this.Rect.Y + (this.Rect.Height - this.TextSize.Height) / 2 + 1f);
                //}
            }
        }

        public virtual void OnMouseDown(MouseEventArgs e)
        {
            if (MouseClick != null)
            {
                MouseClick(this, e);
            }
        }
    }

}
